package src.unapec.seguridad.ejemplos.EncriptacionString;

import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import src.unapec.seguridad.ejemplos.Utils.Key;

public class EncriptacionVisual extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JTextField jTextField = null;

	private JLabel jLabel = null;

	private JScrollPane jScrollPane = null;

	private JLabel jLabel1 = null;

	private JButton jButton = null;
	
	private DefaultTableModel tableModel = new DefaultTableModel();

	private JTable jTable = null;
		
	private Encriptacion encriptacion = new Encriptacion();  //  @jve:decl-index=0:
	
	private Key key = new Key();

	/**
	 * This is the default constructor
	 */
	public EncriptacionVisual() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		
		this.setSize(318, 310);
		this.setContentPane(getJContentPane());
		this.setTitle("Give me words!");
		tableModel.addColumn("Decrypt");
		jTable.setModel(tableModel);
		
	}

	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(21, 218, 273, 54));
			jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel1.setText("Decrypted Text");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(18, 7, 96, 21));
			jLabel.setText("Text:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJTextField(), null);
			jContentPane.add(jLabel, null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(getJButton(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(55, 6, 237, 24));
		}
		return jTextField;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			jScrollPane.setBounds(new Rectangle(18, 69, 278, 140));
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(18, 31, 277, 36));
			jButton.setText("Add Word");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					String srt = jTextField.getText();
					
					if(srt.length() <= 0){
						
						JOptionPane pane = new JOptionPane();
						pane.showMessageDialog(null, "The Textfiel couldn't be empty", "Error", JOptionPane.ERROR_MESSAGE);
						
						
					}else{
						
						
						Object[] value = {encriptacion.encrypt(srt, key.getKeys(1))};
						tableModel.addRow(value);		
						jTextField.setText("");
						
					}
				
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			jTable = new JTable();
			jTable.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					
					String encode = jTable.getModel().getValueAt(jTable.getSelectedRow(), jTable.getSelectedColumn()).toString();
				
					jLabel1.setText(encriptacion.decrypt(encode, key.getKeys(1)));
					
					
				}
			});
		}
		return jTable;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
